package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Announcements;
import company.service.AnnouncementsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class AnnouncementsController {
    @Resource
    AnnouncementsService announcementsService;

    /*
     * 分页查询
     * */
    @RequestMapping("/selectAllAnnouncements/{page}")
    public R selectAllActivties(@PathVariable("page") int page, @RequestBody Announcements announcements){
        QueryWrapper<Announcements> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(announcements.getTitle()!="" && !("".equals(announcements.getTitle())),"title",announcements.getTitle());
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,2);
        List<Announcements> list=this.announcementsService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
     * 新增活动
     * */
    @PostMapping("/AddAnnouncements")
    public R AddAnnouncements(@RequestBody Announcements announcements){
        this.announcementsService.save(announcements);
        return new R();
    }

    /*
     * 活动删除
     * */
    @RequestMapping("/DelAnnouncements/{id}")
    public R DelAnnouncements(@PathVariable("id") int id){
        boolean remove = this.announcementsService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据活动id查询
     * */
    @PostMapping("/selectAnnouncementsbyid")
    public R selectAnnouncementsbyid(@RequestBody Announcements announcements){
        Announcements byId = this.announcementsService.getById(announcements.getAnnouncementid());
        return new R().add("byId",byId);
    }

    /*
     * 修改活动
     * */
    @PostMapping("/uploadAnnouncements")
    public R uploadAnnouncements(@RequestBody Announcements announcements){
        this.announcementsService.updateById(announcements);
        return new R();
    }

    /*
    * 查询活动
    * */
    @RequestMapping("/selectAll")
    public R selectAll(){
        List<Announcements> list = this.announcementsService.list();
        return new R().add("list",list);
    }
}
